package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;


    @Override
    public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {
        OrderSubmitVO submitVO = new OrderSubmitVO();
        BeanUtils.copyProperties(ordersSubmitDTO,submitVO);
        // 判断购物车是否为空
        ShoppingCart query= new ShoppingCart();
        query.setUserId(BaseContext.threadLocaUserlId.get());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByAllshoppingCart(query);
        if (shoppingCarts == null || shoppingCarts.size() == 0) throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);

        //判断收货地址是否为空
        AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
        if (addressBook == null) throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
        //插入订单详情详情表
        Orders orders= new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO,orders);

        orders.setNumber(System.currentTimeMillis()+"");
        orders.setUserId(BaseContext.threadLocaUserlId.get());
        orders.setOrderTime(LocalDateTime.now());
        orders.setStatus(Orders.UN_PAID);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        orders.setOrderTime(LocalDateTime.now());
        orders.setPayStatus(Orders.UN_PAID);
        //插入数据库
        orderMapper.insert(orders);
        // 从购物车获得数据
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart sc : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(sc,orderDetail);
            orderDetail.setOrderId(orders.getId());
            orderDetailList.add(orderDetail);
        }
        //插入订单明细表
        orderDetailMapper.insertOrderDetail(orderDetailList);
        // 清空购物车
        shoppingCartMapper.deleteByUserId(BaseContext.threadLocaUserlId.get());


        // 从插入的数据获得需要的数据
        submitVO.setId(orders.getId());
        submitVO.setOrderAmount(orders.getAmount());
        submitVO.setOrderNumber(orders.getNumber());
        submitVO.setOrderTime(orders.getOrderTime());

        return submitVO;
    }
}























